PART B - step 2: Wiring the Connections

We will now add some clever code to ensure that only one player can be playing at any given moment, thus demonstrating how multiple Beatnik Player instances can effectively be made 'aware' of one another by wiring their event callbacks to a central script. In the enhanced sample below, try the Beatnik control panels again.

"Ambient" by Brian Salter
"Classical" arranged by Brian Salter
"Jungle" by Brian Salter
"Lounge" by Brian Salter
"Modern Rock" by Paul Sebastien

It may appear as though some voodoo spell has been cast to change the fundamental behavior of the player itself. Now when you play one song and then start another song playing without stopping the previous song, the song that was playing stops automatically. We even threw in a graceful fadeout so the previous song doesn't stop abruptly, but instead fades out into the new song.

Take a look at the mystery ingredient that has created this behavior. The code that has been added since the previous step is highlighted in red.

<SCRIPT SRC="music-object.js"></SCRIPT>

<SCRIPT LANGUAGE=JavaScript><!-- //

music1 = new Music ();
music2 = new Music ();
music3 = new Music ();
music4 = new Music ();
music5 = new Music ();

lastPlayed = null;

function handleOnPlay (musicObjectID) {
   if (lastPlayed != null) {
      if (lastPlayed != musicObjectID) {
         lastPlayed.stop (true);
      }
   }
   lastPlayed = musicObjectID;
}

music1.onPlay ('handleOnPlay (music1)');
music2.onPlay ('handleOnPlay (music2)');
music3.onPlay ('handleOnPlay (music3)');
music4.onPlay ('handleOnPlay (music4)');
music5.onPlay ('handleOnPlay (music5)');

// --></SCRIPT>

<CENTER>
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=6>
   <TR BGCOLOR=EEEEEE>
      <TD>
         "<B>Ambient</B>" by Brian Salter
      </TD>
      <TD>
         <SCRIPT LANGUAGE=JavaScript><!-- //
            music1.magicEmbed (
               'SRC="ambient.rmf" ' +
               'WIDTH=144 ' +
               'HEIGHT=15 ' +
               'DISPLAY=SONG ' +
               'AUTOSTART=FALSE ' +
               'LOOP=FALSE'
            );
         // --></SCRIPT>
      </TD>
   </TR>
   <TR BGCOLOR=DDDDDD>
      <TD>
         "<B>Classical</B>" arranged by Brian Salter
      </TD>
      <TD>
         <SCRIPT LANGUAGE=JavaScript><!-- //
            music2.magicEmbed (
               'SRC="classical.rmf" ' +
               'WIDTH=144 ' +
               'HEIGHT=15 ' +
               'DISPLAY=SONG ' +
               'AUTOSTART=FALSE ' +
               'LOOP=FALSE'
            );
         // --></SCRIPT>
      </TD>
   </TR>
   <TR BGCOLOR=CCCCCC>
      <TD>
         "<B>Jungle</B>" by Brian Salter
      </TD>
      <TD>
         <SCRIPT LANGUAGE=JavaScript><!-- //
            music3.magicEmbed (
               'SRC="jungle.rmf" ' +
               'WIDTH=144 ' +
               'HEIGHT=15 ' +
               'DISPLAY=SONG ' +
               'AUTOSTART=FALSE ' +
               'LOOP=FALSE'
            );
         // --></SCRIPT>
      </TD>
   </TR>
   <TR BGCOLOR=BBBBBB>
      <TD>
         "<B>Lounge</B>" by Brian Salter
      </TD>
      <TD>
         <SCRIPT LANGUAGE=JavaScript><!-- //
            music4.magicEmbed (
               'SRC="lounge.rmf" ' +
               'WIDTH=144 ' +
               'HEIGHT=15 ' +
               'DISPLAY=SONG ' +
               'AUTOSTART=FALSE ' +
               'LOOP=FALSE'
            );
         // --></SCRIPT>
      </TD>
   </TR>
   <TR BGCOLOR=AAAAAA>
      <TD>
         "<B>Modern Rock</B>" by Paul Sebastien
      </TD>
      <TD>
         <SCRIPT LANGUAGE=JavaScript><!-- //
            music5.magicEmbed (
               'SRC="modern-rock.rmf" ' +
               'WIDTH=144 ' +
               'HEIGHT=15 ' +
               'DISPLAY=SONG ' +
               'AUTOSTART=FALSE ' +
               'LOOP=FALSE'
            );
         // --></SCRIPT>
      </TD>
   </TR>
</TABLE>
</CENTER>
<P>

In the Cross-connecting a Player and HTML Form Elements example earlier in this tutorial you have seen how functions can be registered as callback handlers for events such as onPause, onPlay, and OnStop.

Well, in addition to being able to take a function as a parameter, the onPause, onPlay, and onStop methods can also take a string as a parameter. This string represents a JavaScript statement (or block of statements) to be executed when the event occurs. In our example, we can now use the same function to handle the callbacks for each of the players, but passing a different parameter to allow the function to discriminate - a code efficiency which simplifies our task tremendously.

See the block of JavaScript code where the onPlay callbacks are registered for the Music Object instances. Each one will end up calling the handleOnPlay function, but with the calling Music Object instance sent as a parameter. For example, music4.onPlay ('handleOnPlay (music4)') will result in the handleOnPlay function being executed every time the Music Object instance called music4 enters into play mode. However, since the statement to be executed is handleOnPlay (music4), when handleOnPlay gets called it will receive music4 as a parameter telling it that the onPlay event was triggered by that instance.

This now leads to the intelligence behind our newly acquired player 'exclusivity' behavior. We can use this central function, handleOnPlay, to maintain a state where only one player can be playing at a time. The way the function does this is to remember the last Music Object instance that was playing and, when a new player enters into play mode, the last playing instance is instructed to stop.

The last playing Music Object is stored in the variable lastPlayed, which is initially set to the value null. The handleOnPlay function performs these two important steps...

  1. It uses the lastPlayed variable to turn off the currently playing instance.

  2. It sets the lastPlayed variable to the instance that has just started playing so that, when in the future handleOnPlay is triggered by another player instance, it will again be able to stop the previously playing song.

There are two additional and more subtle steps that are performed by the function to avoid any quirky behavior.

  1. It checks to see that the lastPlayed variable is not set to null. This would be the case when the page first initializes. If lastPlayed is set to null it means effectively that there is no instance currently playing music and it makes no sense to try to stop an instance playing when there is no instance playing. In fact, trying to call the stop method of an instance variable that is null will result in JavaScript errors - not a desired effect. A simple JavaScript if statement avoids this.

  2. It checks to see that the instance that has just triggered the onPlay event isn't the same instance that last started playing. How could an onPlay event be triggered by the same instance more than once in a row? Simple: we're not capturing or paying any attention to onPause events, and each time a player switches out of pause mode and back into play mode the onPlay event is triggered.

    So, as an example, the user starts the instance music3 playing and an onPlay event is triggered. They then click on the pause button and put the instance music3 into a paused state. Finally, they once again click on the pause button and music3 toggles out of pause mode and enters back into play mode. Once again, an onPlay event is triggered from the instance music3.

    Now, if the instance that just triggered the onPlay event is the same instance that last started playing, no action will be taken. This is done with a simple if condition. Why is this little step so important and why would it be a problem if action were actually taken? Well, whenever an onPlay callback is handled by our handleOnPlay function, it uses the lastPlayed variable to stop the player instance that was playing previously. However, using the scenario that we just described above, when the user unpauses an instance a new onPlay callback will occur, but this time the lastPlayed variable will be exactly the same as the player instance that was unpaused. In this case, if the function actually stops the lastPlayed instance, the player will not leave pause mode and continue playing as the user would expect, but will simply stop dead - not very intuitive behavior. The if condition fixes this.